package com.shine.icis.action;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.blo.IDataCheckService;
import com.shine.icis.blo.IMetadataCheckLogService;
import com.shine.icis.blo.ISubmitCalendarService;
import com.shine.icis.blo.ISubmitConfService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.DictOption;
import com.shine.icis.exception.ICISException;
import com.shine.icis.hbean.DataCheckDef;
import com.shine.icis.hbean.MetadataCheckItem;
import com.shine.icis.hbean.SubmitCalendar;
import com.shine.icis.hbean.SubmitConf;
import com.shine.icis.util.EFRameInf;
import com.shine.icis.util.FTPHelper;

public class SubmitConfAction extends BaseAction {

	/**
	 * 报送配置
	 */
	private static final long serialVersionUID = 7638121893867812633L;

	private ISubmitConfService submitConfService;
	private ISubmitCalendarService submitCalendarService;
	private IMetadataCheckLogService metadataCheckLogService;
	private IDataCheckService dataCheckService;

	// input param
	private int pageNo;
	private int pageSize;
	private List<String> confStrs;

	private String startDateId;
	private String endDateId;
	
	private String dateId;
	private String tradeFlag;
	private String incrementFlag;
	
	private List<String> mciStrs;
	private List<String> dcdStrs;

	// output value
	private List<SubmitConf> submitConfs;
	private List<DictOption> deptOptions;
	private Collection<DictOption> prepLevelOptions;
	
	private List<MetadataCheckItem> metadataCheckItems;   //类别
	private List<DataCheckDef> dataCheckDefs;

	private ListPage<SubmitCalendar> submitCalendarPage;// 日历查询结果
	private String bakDir;// 报送文件备份目录
	
	private String expA13Only;  //0：方案二，1：方案三
	

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setStartDateId(String startDateId) {
		this.startDateId = startDateId;
	}

	public void setEndDateId(String endDateId) {
		this.endDateId = endDateId;
	}

	public void setConfStrs(List<String> confStrs) {
		this.confStrs = confStrs;
	}

	public void setSubmitConfService(ISubmitConfService submitConfService) {
		this.submitConfService = submitConfService;
	}

	public void setSubmitCalendarService(
			ISubmitCalendarService submitCalendarService) {
		this.submitCalendarService = submitCalendarService;
	}

	public void setMetadataCheckLogService(
			IMetadataCheckLogService metadataCheckLogService) {
		this.metadataCheckLogService = metadataCheckLogService;
	}
	
	public void setDataCheckService(IDataCheckService dataCheckService) {
		this.dataCheckService = dataCheckService;
	}
	
	public void setDateId(String dateId) {
		this.dateId = dateId;
	}

	public void setTradeFlag(String tradeFlag) {
		this.tradeFlag = tradeFlag;
	}

	public void setIncrementFlag(String incrementFlag) {
		this.incrementFlag = incrementFlag;
	}

	public void setMciStrs(List<String> mciStrs) {
		this.mciStrs = mciStrs;
	}

	public void setDcdStrs(List<String> dcdStrs) {
		this.dcdStrs = dcdStrs;
	}

	public List<SubmitConf> getSubmitConfs() {
		return submitConfs;
	}

	public List<DictOption> getDeptOptions() {
		return deptOptions;
	}

	public Collection<DictOption> getPrepLevelOptions() {
		return prepLevelOptions;
	}

	public List<MetadataCheckItem> getMetadataCheckItems() {
		return metadataCheckItems;
	}
	
	public List<DataCheckDef> getDataCheckDefs() {
		return dataCheckDefs;
	}
	
	public String getBakDir() {
		return bakDir;
	}

	public ListPage<SubmitCalendar> getSubmitCalendarPage() {
		return submitCalendarPage;
	}

	public String getExpA13Only() {
		return expA13Only;
	}

	/**
	 * 加载报送配置
	 * 
	 * @return
	 */
	public String loadSubmitConfs() {
		try {
			bakDir = FileDispatcher.getSaveDir(FileDispatcher
					.getAbsPath(SysConstants.SUBMIT_FILES_DIR));
			this.expA13Only = SysCodeDictLoader.getInstance().getSubmitConfValue("EXP_A13_ONLY");
			submitConfs = submitConfService.getSubmitConfByType(
					SubmitConf.DATA_SUBMIT, SubmitConf.STATUS_EDITABLE);
			prepLevelOptions = SysCodeDictLoader.getInstance()
					.getPrepLevelOptions();
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}

	}

	public String saveSubmitConfs() {
		if (confStrs == null || confStrs.size() == 0) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			Map<String, String> map = new HashMap<String, String>();
			for (String str : confStrs) {
				if (str == null) {
					continue;
				}
				String[] tem = str.split(";");
				if (tem.length != 2) {
					return error("数据格式不对");
				}
				if (tem[0].equals("FILE_SAVE_PATH")) {
					if (tem[1].toLowerCase().startsWith("ftp://")) {
						FTPHelper.testFTP(tem[1]);
					} else {
						File f = new File(tem[1]);
						if (!f.exists() && !f.mkdirs()) {
							return error("文件保存路径不正确，请重新选择");
						}
					}
				}
				if (tem[0].equals("PROP_RDY_FILENAME")) {
					String[] prf = tem[1].split("-");
					if ("true".equals(prf[0])) {
						map.put(tem[0], prf[1]);
					} else {
						map.put(tem[0], null);
					}
					continue;
				}
				map.put(tem[0], tem[1]);
			}

			submitConfService.modifySubmitConfs(map);
			success = true;
			return success("保存成功！");
		} catch (ICISException e) {
			return error(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam) getSession().getAttribute(
					SysConstants.SESSION_USER_KEY);
			// 记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(),
					EFRameInf.FUNCCODE_SUBMITCFG, "保存报送配置信息", success);
		}
	}

	/**
	 * 报送日历页面初始化
	 * */
	public String initSubmitCalendar() {
		return success();
	}
	
	/**
	 * 报送列表查询
	 * */
	public String querySubmitCalendar() {
		QueryParameters qp = new QueryParameters();

		// 设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		// 排序信息
		qp.addOrder("dateId", true);
		// 参数

		if (startDateId != null && !startDateId.trim().equals("")) {
			qp.addParameter("startDateId", Integer.parseInt(startDateId.trim()
					.replaceAll("-", "")));
		}
		if (endDateId != null && !endDateId.trim().equals("")) {
			qp.addParameter("endDateId", Integer.parseInt(endDateId.trim()
					.replaceAll("-", "")));
		}

		try {
			this.submitCalendarPage = submitCalendarService
					.getSubmitCalendarPage(qp);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}

	/**
	 * 报送数据修改
	 * */
	public String modifySubmitCalendar() {
		if (this.dateId == null || this.tradeFlag == null || this.incrementFlag == null) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			SubmitCalendar s = new SubmitCalendar();
			s.setDateId(Integer.parseInt(dateId));
			s.setTradeFlag(Integer.parseInt(tradeFlag));
			s.setIncrementFlag(Integer.parseInt(incrementFlag));
			this.submitCalendarService.modifySubmitCalendar(s);
			success = true;
			return success();
		} catch(ICISException e) {
			return error(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam) getSession().getAttribute(
					SysConstants.SESSION_USER_KEY);
			// 记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_SUBMITCFG, "保存报送日历维护信息", success);
		}
	}
	
	/**
	 * 数据检查配置页面初始化
	 * */
	public String initSubmitCheck() {
		metadataCheckItems = metadataCheckLogService.getMetadataCheckItems(MetadataCheckItem.ENABLE);
		dataCheckDefs = dataCheckService.getDataCheckDefs(DataCheckDef.ENABLE);
		return success();
	}
	
	public String modifySubmitCheck() {
		if (mciStrs == null || dcdStrs == null) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			Map<Integer, Character> mciMap = new HashMap<Integer, Character>();
			for (String str : mciStrs) {
				String[] tem = str.split(";");
				mciMap.put(Integer.parseInt(tem[0]), tem[1].charAt(0));
			}
			Map<Integer, Character> dcdMap = new HashMap<Integer, Character>();
			for (String str : dcdStrs) {
				String[] tem = str.split(";");
				dcdMap.put(Integer.parseInt(tem[0]), tem[1].charAt(0));
			}

			submitConfService.modifyCheckConfs(mciMap, dcdMap);
			success = true;
			return success("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam) getSession().getAttribute(
					SysConstants.SESSION_USER_KEY);
			// 记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(),
					EFRameInf.FUNCCODE_SUBMITCFG, "保存报送配置信息", success);
		}
	}
}
